<?xml version = '1.0' encoding = 'UTF-8'?>
<HBRRepo><variables><variable name="OQP_Territory" type="member" usage="const" id="1" product="Planning"><property name="application">SalesPln</property><property name="dimensionInputMode">type</property><property name="dimensionType">Territory</property><property name="prompt_text">LABEL_QTP_SLS_PLAN_PRMPT_TERRITORY</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable><variable name="OQP_Account" type="member" usage="const" id="2" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="dimension">Accounts</property><property name="prompt_text">Enter Account</property><property name="scope">ruleset</property><property name="seeded">true</property><value/><limits type="expression"><property name="value">@Relative("OEP_All Accounts",0)</property></limits></variable><variable name="OQP_Product" type="member" usage="const" id="3" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="dimension">Product</property><property name="prompt_text">Enter Quota Product</property><property name="scope">ruleset</property><property name="seeded">true</property><value/><limits type="expression"><property name="value">@Relative("OEP_All Product",0)</property></limits></variable><variable name="OQP_QuotaAmt" type="integer" usage="const" id="4" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="prompt_text">Enter Quota Amount</property><property name="scope">ruleset</property><property name="seeded">true</property><value/></variable></variables><rulesets/><rules><rule id="1" name="OQP_BottomUp Add Product" product="Planning"><property name="application">SalesPln</property><property name="plantype">OEP_QTP</property><property name="seeded">true</property><variable_references><variable_reference name="OQP_Territory" id="1"><property name="application">SalesPln</property><property name="hidden">true</property><property name="rule_name">OQP_BottomUp Add Product</property><property name="seq">1</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">"No Entity"</property></variable_reference><variable_reference name="OQP_Account" id="2"><property name="application">SalesPln</property><property name="hidden">false</property><property name="plantype">OEP_QTP</property><property name="rule_name">OQP_BottomUp Add Product</property><property name="seq">2</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">"OEP_Any Accounts"</property></variable_reference><variable_reference name="OQP_Product" id="3"><property name="application">SalesPln</property><property name="hidden">false</property><property name="plantype">OEP_QTP</property><property name="rule_name">OQP_BottomUp Add Product</property><property name="seq">3</property><property name="type">3</property><property name="useAsOverrideValue">false</property><property name="validation_value">OEP_Any Product</property></variable_reference><variable_reference name="OQP_QuotaAmt" id="4"><property name="application">SalesPln</property><property name="hidden">false</property><property name="plantype">OEP_QTP</property><property name="rule_name">OQP_BottomUp Add Product</property><property name="seq">4</property><property name="type">18</property><property name="useAsOverrideValue">false</property><property name="validation_value">100</property></variable_reference></variable_references><script type="groovy">		/*RTPS:{OQP_Territory}  {OQP_Account} {OQP_Product} {OQP_QuotaAmt}*/
		/*PURPOSE: This business rule adds new product with Quota when user wants to move Quota across in to different products*/
		def grid=operation.grid;
		List&lt;String> PovMembers=[];
		List&lt;String> ColumnDimensions=[];
        List&lt;String> CustomDimMembers=[];
		List&lt;String> AggDimensions=[];
        List&lt;String> AnyMbrs = [];
		String strCrossDim  = "";
        String strCrossDimAny="";
		String strFirstCrossDim="";
		int firstColumn=0;
		boolean isUnitsModel =false;
        boolean isExistingMbr=false;
		String driverDimMember;
		Dimension product=rtps.OQP_Product.member.getDimension()
        Dimension Account=rtps.OQP_Account.member.getDimension()
		StringBuilder cscript = new StringBuilder();
		/*try
		{*/
        String strProduct= rtps.OQP_Product.member;    
        
        /*Evalute If  appliction types is Units or Revenue  */
			Application app=operation.getApplication();
             Cube qtp=operation.getCube(); 
              Dimension dim=app.getDimension(DimensionType.ACCOUNT,qtp);
              if (dim.hasMember("OQP_Units",qtp)==true)
              {
                isUnitsModel=true;
                driverDimMember="OQP_Last Year Units";
              }
              else
              {
                isUnitsModel=false;
                driverDimMember="OQP_Last Year Revenue";                    
              }
              
              /*Fetch POV member List*/	
          List&lt;DataGrid.HeaderCell> pov=grid.getPov()
			for (DataGrid.HeaderCell cell : pov)
			{
			   String mbrName = cell.getEssbaseMbrName();
			   DimensionType dimType = cell.getDimensionType();
			   String dimName=cell.getDimName(); 
			   
				if(dimType==DimensionType.CUSTOM)
				{					
					AggDimensions.add('''"'''+dimName+'''"''')
                    CustomDimMembers.add('''"'''+mbrName+'''"''')
					strCrossDim= strCrossDim + "\"OEP_All "+ dimName+"\"->"
					AnyMbrs.add('''"No '''+dimName+'''"''')
					strCrossDimAny=strCrossDimAny + "\"No "+ dimName+"\"->"
				}
				else
				{
					 PovMembers.add('''"'''+mbrName+'''"''' )							
				}
				
			}

		/*Fetch column List */
		List&lt;List&lt;DataGrid.HeaderCell>> cols = grid.getColumns();
		for (List&lt;DataGrid.HeaderCell> cells : cols) 
		{		 
			for (DataGrid.HeaderCell cell : cells)  
			  {       
			   String dimName = cell.getDimName();
			   String memName = cell.getMbrName();
               DimensionType DimType= cell.getDimensionType()
               if (memName.equals(strProduct))				   
                 {
						isExistingMbr=true                           
                        break;
				 }     	 
			   if (!ColumnDimensions.contains(dimName))
				  {
					if (!DimType.equals(DimensionType.ACCOUNT) &amp;&amp; !DimType.equals(DimensionType.PERIOD) )
					{
					
						ColumnDimensions.add(dimName);   
						if (!dimName.equals(product.getName()))
						{
						strCrossDimAny= strCrossDimAny + "\"No "+ dimName+"\"->";
                        strCrossDim= strCrossDim + "\"OEP_All "+ dimName+"\"->"
						}
						AggDimensions.add('''"'''+dimName+'''"''');
					}			   
			     }
			 }     
		 }
          if (isExistingMbr==true )
            {
            	throwVetoException("This Product is already assigned to the Territory or Resource. Please select new Product");            
            }
            
            
         
         /*Start of Main FIx*/
		cscript.append('''SET AGGMISSG ON;\n''');
		cscript.append('''SET UPDATECALC OFF;\n'''); 
		cscript.append('''FIX(@Relative("Yeartotal",0),'''+PovMembers.join(''',''')+''',"'''+rtps.OQP_Territory.member+'''")\n''')
        /*Fix on Territory dimensoin and Assign Quotao on given product and account */
        if (CustomDimMembers.size()>0)
        {
			cscript.append('''	FIX('''+CustomDimMembers.join(''',''')+''')\n''');
        }
		cscript.append('''		"OQP_Quota"(\n''');
		cscript.append('''			"'''+rtps.OQP_Product.member+'''"->"'''+rtps.OQP_Account.member+'''"->"OQP_Quota"='''+ rtps.OQP_QuotaAmt.getDoubleValue()/12 +''';\n''');

		cscript.append('''			)\n''');
        if (CustomDimMembers.size()>0)
        {
			cscript.append('''	ENDFIX\n''');
        }
        if (Account)
        {
        	AggDimensions.join('''"'''+Account+'''"''');
        }
        if(product)
        {
         strCrossDim= strCrossDim + "\"OEP_All "+ product+"\"->"
        }
		cscript.append('''	FIX("OQP_Quota")\n''');		
        cscript.append('''			AGG(''')
		cscript.append(AggDimensions.join(''',''')+''');\n''')		
		cscript.append('''	ENDFIX\n''');
        cscript.append('''	FIX("'''+rtps.OQP_Account.member+'''",'''+AnyMbrs.join(''',''')+''')\n''')
		cscript.append('''		"OQP_Quota"='''+strCrossDim+'''"OQP_Quota";\n''');
		cscript.append('''	ENDFIX\n''')
        
		cscript.append('''ENDFIX\n''');
		println (cscript.toString())
		return cscript.toString()
		/*}
        catch(all) {
			throw new HspRuntimeException('Error occured while Running  Business Rule.');
		}*/
		
		</script></rule></rules><components/><deployobjects><deployobject product="2" application="salespln" plantype="oep_qtp" obj_id="1" obj_type="1" name="OQP_BOTTOMUP ADD PRODUCT"/></deployobjects></HBRRepo>